package com.restaurant.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.restaurant.dao.admin.AdminDao;
import com.restaurant.entity.Admin;
import com.restaurant.service.intf.AdminService;
import com.restaurant.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminDao adminDao;
    @Override
    public R adminLogin(String username,String password, String captcha, HttpSession session) {
        //1、校验参数是否输入
        if (StringUtils.hasLength(captcha) && StringUtils.hasLength(username)) {
            //2、验证验证码
            if (captcha.equalsIgnoreCase(session.getAttribute("captcha").toString())) {
                //3、查询数据库
                Admin admin1 = adminDao.adminLogin(username);
                //4、验证是否存在
                if (admin1 != null) {
                    //账号存在，验证密码
                    //5、验证密码
                    if (admin1.getPassword().equals(password)) {
                        //成功，登陆成功
                        //6、记录在线的用户
                        session.setAttribute("admin",admin1);
                        return R.ok();
                    }
                }
            }
        }
        return R.fail();
    }
}
